import os
from collections import defaultdict

class GrubConfigParser:
    def __init__(self, simulate_mode=False):
        self.simulate_mode = simulate_mode
        if simulate_mode:
            self.config_data = {
                "GRUB_DEFAULT": "0",
                "GRUB_TIMEOUT": "5",
                "GRUB_CMDLINE_LINUX": "quiet splash"
            }
        else:
            self.config_data = self._load_real_config()

    def _load_real_config(self):
        try:
            with open("/etc/default/grub", "r") as f:
                lines = f.readlines()
            config = {}
            for line in lines:
                if line.startswith("GRUB_"):
                    key, value = line.strip().split("=", 1)
                    config[key] = value.strip('"')
            return config
        except FileNotFoundError:
            return {}

    def get_global_config(self):
        return self.config_data

    def save_to_file(self, path):
        if self.simulate_mode:
            with open(path, "w") as f:
                for k, v in self.config_data.items():
                    f.write(f'{k}="{v}"\n')
            return True
        return False